{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test CodeAutoMergeEditBlock\n",
    "\n",
    "This notebook tests the CodeAutoMergeEditBlock class's ability to parse different code block formats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autocoder.common.code_auto_merge_editblock import CodeAutoMergeEditBlock\n",
    "from autocoder.common import AutoCoderArgs\n",
    "import byzerllm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test data with two different formats\n",
    "test_input = \"\"\"\n",
    "```python\n",
    "##File: src/autocoder/index/index.py\n",
    "<<<<<<< SEARCH\n",
    "    # Print final statistics\n",
    "    logger.info(\"\\n=== Build Index and Filter Files Summary ===\\n\")\n",
    "    return source_code\n",
    "=======\n",
    "    # Print final statistics\n",
    "    logger.info(\"\\n=== Build Index and Filter Files Summary ===\\n\")\n",
    "    # Send filter end event\n",
    "    if args.request_id:\n",
    "        queue_communicate.send_event_no_wait(\n",
    "            request_id=args.request_id,\n",
    "            event=CommunicateEvent(\n",
    "                event_type=CommunicateEventType.CODE_INDEX_FILTER_END.value,\n",
    "                data=f\"Completed filtering. Final files selected: {stats['final_files']}\"\n",
    "            )\n",
    "        )\n",
    "    return source_code\n",
    ">>>>>>> REPLACE\n",
    "```\n",
    "\n",
    "```python:src/autocoder/index/index.py\n",
    "<<<<<<< SEARCH\n",
    "    # Print final statistics\n",
    "    logger.info(\"\\n=== Build Index and Filter Files Summary ===\\n\")\n",
    "    return source_code\n",
    "=======\n",
    "    # Print final statistics\n",
    "    logger.info(\"\\n=== Build Index and Filter Files Summary ===\\n\")\n",
    "    # Send filter end event\n",
    "    if args.request_id:\n",
    "        queue_communicate.send_event_no_wait(\n",
    "            request_id=args.request_id,\n",
    "            event=CommunicateEvent(\n",
    "                event_type=CommunicateEventType.CODE_INDEX_FILTER_END.value,\n",
    "                data=f\"Completed filtering. Final files selected: {stats['final_files']}\"\n",
    "            )\n",
    "        )\n",
    "    return source_code\n",
    ">>>>>>> REPLACE\n",
    "```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-11-30 21:39:36.770\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-11-30 21:39:36,828\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-11-30 21:39:36,857\tINFO worker.py:1740 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All tests passed!\n"
     ]
    }
   ],
   "source": [
    "# Initialize CodeAutoMergeEditBlock\n",
    "llm = byzerllm.ByzerLLM.from_default_model(\"deepseek_chat\")\n",
    "args = AutoCoderArgs()\n",
    "merger = CodeAutoMergeEditBlock(llm, args)\n",
    "\n",
    "# Parse the test input\n",
    "results = merger.parse_whole_text(test_input)\n",
    "\n",
    "# Verify results\n",
    "assert len(results) == 2, f\"Expected 2 results, got {len(results)}\"\n",
    "\n",
    "# Both results should have the same path\n",
    "assert results[0].path == \"src/autocoder/index/index.py\"\n",
    "assert results[1].path == \"src/autocoder/index/index.py\"\n",
    "\n",
    "# Both results should have the same content structure\n",
    "for result in results:\n",
    "    assert \"<<<<<<< SEARCH\" in result.content\n",
    "    assert \"=======\" in result.content\n",
    "    assert \">>>>>>> REPLACE\" in result.content\n",
    "    \n",
    "print(\"All tests passed!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Edit extraction tests passed!\n"
     ]
    }
   ],
   "source": [
    "# Let's also test the get_edits method to ensure we can extract the search and replace blocks correctly\n",
    "edits = merger.get_edits(test_input)\n",
    "\n",
    "# We should have 2 edits\n",
    "assert len(edits) == 2, f\"Expected 2 edits, got {len(edits)}\"\n",
    "\n",
    "# Each edit should be a tuple of (path, head, update)\n",
    "for path, head, update in edits:\n",
    "    assert path == \"src/autocoder/index/index.py\"\n",
    "    assert \"# Print final statistics\" in head\n",
    "    assert \"# Send filter end event\" in update\n",
    "    \n",
    "print(\"Edit extraction tests passed!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
